from google.appengine.ext import webapp
import re
register = webapp.template.create_template_register()

def add_zeros(value):
  value = int(value)
  if value<10:
    return '0'+str(value)
  else:
    return str(value)

register.filter(add_zeros)

def datetostring(value):
  strmonth = 'nop'
  if str(value.month)=='1':
    strmonth = 'Ene'
  if str(value.month)=='2':
    strmonth = 'Feb'
  if str(value.month)=='3':
    strmonth = 'Mar'
  if str(value.month)=='4':
    strmonth = 'Apr'
  if str(value.month)=='5':
    strmonth = 'May'
  if str(value.month)=='6':
    strmonth = 'Jun'
  if str(value.month)=='7':
    strmonth = 'Jul'
  if str(value.month)=='8':
    strmonth = 'Ago'
  if str(value.month)=='9':
    strmonth = 'Sept'
  if str(value.month)=='10':
    strmonth = 'Oct'
  if str(value.month)=='11':
    strmonth = 'Nov'
  if str(value.month)=='12':
    strmonth = 'Dic'
  
  return strmonth + ' ' + str(value.day)

register.filter(datetostring)


_base_js_escapes =  (
    ('\\', r'\u005C'),
    ('\'', r'\u0027'),
    ('"', r'\u0022'),
    ('>', r'\u003E'),
    ('<', r'\u003C'),
    ('&', r'\u0026'),
    ('=', r'\u003D'),
    ('-', r'\u002D'),
    (';', r'\u003B'),
    (u'\u2028', r'\u2028'),
    (u'\u2029', r'\u2029')
)

# Escape every ASCII character with a value less than 32.
_js_escapes = (_base_js_escapes +
               tuple([('%c' % z, '\\u%04X' % z) for z in range(32)]))

def escapejs(value):
    """Hex encodes characters for use in JavaScript strings."""
    for bad, good in _js_escapes:
        value = value.replace(bad, good)
    return value

register.filter(escapejs)


def slugify(string):
    string = re.sub('\s+', '_', string)
    string = re.sub('[^\w.-]', '', string)
    return string.strip('_.- ').lower()

register.filter(slugify)
